package com.karson.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.karson.common.config.redis.RedisConfig;
import com.karson.common.constants.BusinessConstants;
import com.karson.common.constants.Constants;
import com.karson.common.dto.rabbitmq.TagStatusMessageDTO;
import com.karson.common.exception.AsmrException;
import com.karson.common.model.ArAlbumTag;
import com.karson.common.util.RedisUtil;
import com.karson.service.service.AlbumTagsService;
import com.karson.service.dao.AlbumTagsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* @author ZKS
* @description 针对表【ar_album_tags】的数据库操作Service实现
* @createDate 2025-07-28 15:25:53
*/
@Service
public class AlbumTagsServiceImpl extends ServiceImpl<AlbumTagsMapper, ArAlbumTag>
    implements AlbumTagsService{

    @Autowired
    private AlbumTagsMapper albumTagsMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public void flushTagStatus(TagStatusMessageDTO message) {
        String key = RedisConfig.COUNT_KEY + BusinessConstants.BUSINESS_TAG + ":" + message.getTagId() + ":" + message.getAlbumId();
        Integer likeCount = (Integer) redisUtil.hmGet(key, RedisConfig.HASH_LIKE_KEY);
        Integer dislikeCount = (Integer) redisUtil.hmGet(key, RedisConfig.HASH_DISLIKE_KEY);

        if (likeCount == null) {
            likeCount = 0;
        }
        if (dislikeCount == null) {
            dislikeCount = 0;
        }
        int score = likeCount - dislikeCount;

        LambdaUpdateWrapper<ArAlbumTag> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ArAlbumTag::getAlbumId, message.getAlbumId())
                .eq(ArAlbumTag::getTagId, message.getTagId());
        ArAlbumTag arAlbumTag = albumTagsMapper.selectOne(wrapper);
        // 写数据库
        if (arAlbumTag == null) {
            throw new AsmrException("找不到该标签");
        }
        arAlbumTag.setAgreeCount(likeCount);
        arAlbumTag.setDisagreeCount(dislikeCount);
        arAlbumTag.setVoteScore(score);
        // 根据你的规则更新状态
        String newStatus;
        if (!arAlbumTag.getIsDefault()){
            // 只有投票中的标签才需要进来计算状态
            if (score >= 10) {
                newStatus = Constants.TAG_NORMAL;
            } else if (score <= -10) {
                newStatus = Constants.TAG_REJECT;
            } else {
                newStatus = Constants.TAG_LOW_ORIGINALITY;
            }
            arAlbumTag.setStatus(newStatus);
        }
        update(arAlbumTag, wrapper);
    }

    @Override
    public List<Integer> getTagListWithAlbumId(Integer albumId) {
        LambdaQueryWrapper<ArAlbumTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArAlbumTag::getAlbumId,albumId);
        List<ArAlbumTag> albumTagList = list(queryWrapper);
        return albumTagList.stream().map(ArAlbumTag::getTagId).toList();
    }

    @Override
    public List<Integer> getAlbumsByTagIds(List<Integer> idList) {
        return albumTagsMapper.selectList(
                        new QueryWrapper<ArAlbumTag>()
                                .in("tag_id", idList)
                                .ne("status", Constants.TAG_REJECT)
                ).stream()
                .map(ArAlbumTag::getAlbumId)
                .distinct()
                .toList();
    }
    private void updateTagStatus(Integer albumId, Integer tagId, String tagStatus) {
        LambdaUpdateWrapper<ArAlbumTag> arAlbumTagLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        arAlbumTagLambdaUpdateWrapper.eq(ArAlbumTag::getAlbumId,albumId);
        arAlbumTagLambdaUpdateWrapper.eq(ArAlbumTag::getTagId,tagId);
        arAlbumTagLambdaUpdateWrapper.set(ArAlbumTag::getStatus,tagStatus);
        super.update(arAlbumTagLambdaUpdateWrapper);
    }
}




